package com.jvup.common.widget.adapter.auto;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import com.jvup.common.basic.ObjectUtil;
import com.jvup.common.collect.ListUtil;
import com.jvup.common.widget.adapter.auto.SummerComplexBindAdapter.SummerViewHolder;
import com.jvup.common.widget.adapter.auto.annotation.AdapterEntityBind;

import java.util.HashMap;
import java.util.List;

/**
 * 多布局复合自动适配绑定Adapter,适用于基础的RecyclerView
 * @param <T> 用于自动适配的 ViewModel 类型
 */
public class SummerComplexBindAdapter<T extends AutoVariable> extends Adapter<SummerViewHolder> {

	/** ViewModel数据列表 */
	private final List<T> dataList;

	/** 事件处理器 */
	private AutoVariable eventProcess;

	/** 缓存布局的RID */
	private final HashMap<String, Integer> viewLayoutIdMap = new HashMap<>();


	/**
     * @param dataList 包含子项数据的ViewModel
	 */
	public SummerComplexBindAdapter(@NonNull List<T> dataList) {
		this(dataList, null);
	}

	/**
	 * @param dataList 包含子项数据的ViewModel
	 * @param eventProcess 设置给子项绑定的事件处理器
	 */
	public SummerComplexBindAdapter(@NonNull List<T> dataList, AutoVariable eventProcess) {
		this.dataList = dataList;
		this.eventProcess = eventProcess;
	}

	/**
	 * 设置事件处理器到所有项目View并进行绑定
	 * @param eventProcess 事件处理器
	 */
	public void setEventProcess(AutoVariable eventProcess) {
		this.eventProcess = eventProcess;
		notifyDataSetChanged();
	}

	/**
     * 获取所有数据列表
	 * @return viewModel 数据
	 */
	public List<T> getDataList() {
		return dataList;
	}

	/**
     * 获取所有项目的数量，如果列表为NULL则返回0
	 * @return 子项目统计数
	 */
	@Override
	public int getItemCount() {
		return dataList == null ? 0 : dataList.size();
	}

	@Override
	public void onBindViewHolder(@NonNull SummerViewHolder viewHolder, int position) {
		T data = ListUtil.range(dataList, position) ? dataList.get(position) : null;
		viewHolder.bindData(data);
		viewHolder.bindData(eventProcess);
	}

	@Override
	public int getItemViewType(int position) {
		return fetchRootViewId(position);
	}

	/**
	 * 构建一个持有View的ViewHolder
	 * @param viewGroup 父布局
	 * @param viewType	view类型，由于 {@link #getItemViewType(int)} 是通过返回于数据绑定的布局ID为类型所以可以用于直接构建布局
	 * @return
	 */
	@Override
	public SummerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
		LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
		ViewDataBinding viewDataBinding = DataBindingUtil.inflate(layoutInflater, viewType, viewGroup, false);
		SummerViewHolder viewHolder = new SummerViewHolder(viewDataBinding);
		return viewHolder;
	}

	/**
	 * 获得指定位置的布局ID
	 * @return layoutRid
	 */
	private int fetchRootViewId(int position) {
		if(ListUtil.isEmpty(dataList)) {
			return 0;
		}

		T data = ListUtil.range(dataList, position) ? dataList.get(position) : null;
		if(ObjectUtil.isNull(data)) {
			return 0;
		}

		Class<?> dataClass = data.getClass();
		String className = dataClass.getCanonicalName();

		if(viewLayoutIdMap.containsKey(className)) {
			return viewLayoutIdMap.get(className);
		}

		if(dataClass.isAnnotationPresent(AdapterEntityBind.class)) {
			AdapterEntityBind adapterEntityBind = dataClass.getAnnotation(AdapterEntityBind.class);
			int viewLayoutId = adapterEntityBind.viewRid();
			viewLayoutIdMap.put(className, viewLayoutId);
			return viewLayoutId;
		}

		return 0;
	}

	/**
     * 缓存binder的处理器，负责绑定数据
	 */
	protected static class SummerViewHolder extends ViewHolder {
		protected final ViewDataBinding viewDataBinding;

		public SummerViewHolder(@NonNull ViewDataBinding viewDataBinding) {
			super(viewDataBinding.getRoot());
			this.viewDataBinding = viewDataBinding;
		}

		public void bindData(AutoVariable observable) {
			if(observable != null) {
				observable.bindVariable(viewDataBinding);
			}
		}
	}

}